package love.yuanmengrobot.als.business.dao.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:lzl
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/8 14:09
 */

import love.yuanmengrobot.als.business.dao.StudentCreditDao;
import love.yuanmengrobot.als.business.javaBean.StudentViewCredit;
import love.yuanmengrobot.als.utilPublic.DoubleFormat;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;
import love.yuanmengrobot.als.utilPublic.SqlString;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Pattern;

public class StudentCreditDaoImpl implements StudentCreditDao {
    @Override
    public StudentViewCredit getStudentCredit(String stuNo) {
        StudentViewCredit svc=new StudentViewCredit();
        double creditSum=0;//已修总学分
        double optionCreditSum=0;//任选已修
        double limitCreditSum=0;//限选已修
        double tongShiCreditSum=0;//通识已修
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn= JdbcUtil.getConnection();
            ps=conn.prepareStatement(SqlString.selectCredit(stuNo));//通过学号查询某个学生的已修课程的对应成绩、对应学分和课程性质
            rs = ps.executeQuery();
            while (rs.next()) {
                String score = rs.getString("score");
                double credit=rs.getDouble("credit");
                String courseType=rs.getString("course_type");

                if (isInteger(score)) {//成绩为数字
                    int scoreInt = Integer.parseInt(score);
                    if (scoreInt >= 60) {
                        creditSum+=credit;
                        if(courseType.equals("专业任选")){
                            optionCreditSum+=credit;
                        }
                        if(courseType.equals("专业限选")){
                            limitCreditSum+=credit;
                        }
                    }
                } else {//成绩为字符串
                    if ("不及格".equals(score) || "旷考".equals(score) || "取消".equals(score) || "不合格".equals(score)) {
                        System.out.println("成绩不合格,不计分");
                    } else {
                        if(score.equals("合格")){
                            tongShiCreditSum+=credit;
                        }else{
                            creditSum+=credit;
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(rs,ps,conn);
        }
        svc.setLimitCreditSum(DoubleFormat.format(limitCreditSum));
        svc.setOptionCreditSum(DoubleFormat.format(optionCreditSum));
        svc.setTongShiCreditSum(DoubleFormat.format(tongShiCreditSum));
        svc.setStudentCreditSum(DoubleFormat.format(creditSum));
//        System.out.println("限选已修" + format(limitCreditSum));
//        System.out.println("任选已修" + format(optionCreditSum));
//        System.out.println("通识已修" + format(tongShiCreditSum));
//        System.out.println("已修学分" + format(creditSum));
        return svc;
    }
    /*
     * 判断字符串是否可以转化成数字
     * @return true 可以转成数字,false 不能转成数字
     * @str 需要进行判断的字符串*/
    private static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }


}
